/**
 * @Company 
 * @Title: LoginController.java 
 * @Package com.jbinfo.qingcaowan.app.controller 
 * @author lizhenwei
 * @date 2016-07-11 下午1:06:21 
 * @version V1.0   
 */
package com.jbinfo.qingcaowan.app.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.studyManager.dto.NoteDTO;
import com.jbinfo.qingcaowan.studyManager.entity.NoteInfo;
import com.jbinfo.qingcaowan.studyManager.service.ReadingNoteService;
import com.jbinfo.qingcaowan.user.dto.UserDTO;

/**
 * @ClassName: LoginController
 * @Description: 笔记controller
 * @author lizhenwei
 */
/**
 * @author Administrator
 *
 */
@Controller
public class NoteController {

	private static final Logger LOG = LoggerFactory.getLogger(NoteController.class);

	@Resource
	private ReadingNoteService readingNoteService;
	
	@RequestMapping(value = "/{client}/saveNote")
	public @ResponseBody
	Object saveNote(@PathVariable("client") String client
			, UserDTO userDTO, NoteDTO noteDTO) {
		LOG.info("客户端为 " + client + " noteDTO：" + noteDTO.toString());
		
		String userId = userDTO.getUserId();
		
		noteDTO.setNoteAuthor(userId);
		noteDTO.setNoteStatus("1");// 1为显示
		
		NoteInfo noteInfo = ClonePojoUtil.copyClassFromTo(noteDTO, NoteInfo.class);
		
		return readingNoteService.saveNoteInfo(noteInfo);
	}
	
	// 阅读时查询笔记信息
	@RequestMapping(value = "/{client}/findReadNote")
	public @ResponseBody
	Object findReadNote(@PathVariable("client") String client
			, UserDTO userDTO, NoteDTO noteDTO) {
		LOG.info("客户端为 " + client + ", userId: " + userDTO.getUserId() + ", noteDTO：" + noteDTO.toString());
		
		String userId = userDTO.getUserId();
		
		noteDTO.setNoteAuthor(userId);
//		noteDTO.setNoteStatus("0");// 0为显示
		if (noteDTO.getStrNoteDate() == null 
				|| noteDTO.getStrNoteDate().isEmpty()) {
			// 取当前日期
			Date dNowDate = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strNowDate = sdf.format(dNowDate);
			try {
				dNowDate = sdf.parse(strNowDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				LOG.error("日期转化失败： " + e.getMessage());
			}
			noteDTO.setNoteDate(dNowDate);
		}
		
		return readingNoteService.findNoteInfoByParam(noteDTO);
	}
}
